<?php
/**
 * Created by PhpStorm.
 * User: 王泽锋
 * Date: 2018/03/14
 * Time: 下午 13:37
 */

namespace app\home\controller;

use app\home\model\Shopping_cart as CartModel;
use app\home\model\Shopping_commodity as CommodityModel;
class Cart extends Base
{
    /**
     * 购物车主页
     */
    public function index()
    {
        $userId = session('userId');
        $CartModel = new CartModel();
        $map = ['create_user' => ['eq', $userId], 'status' => ['eq', 0]];
        $data = $CartModel->get_list($map);
        $list=$CartModel::where($map)->count();

        $this->assign('data', $data);
        $this->assign('list', $list);
        return $this->fetch();
    }


    /**
     * 购物车主页列表加载更多
     */
    public function listmore()
    {
        $userId = session('userId');
        $CartModel = new CartModel();
        $len = input('length');
        $map = ['create_user' => ['eq', $userId],'status' => ['eq', 0]];
        $list = $CartModel->get_lists($map,$len);
        foreach ($list as $v) {
            $v['price'] = number_format($v['price'], 2, ".", "");
            $v['price1'] = floor($v['price']);
            $v['price2'] = mb_substr($v['price'], -2);
        }
        if ($list) {
            return $this->success('加载成功', '', $list);
        } else {
            return $this->error('加载失败');
        }
    }


    /**
     * 加入购物车
     */
    public function add()
    {
        $data['cid']=input('id');
        $data['create_user'] = session('userId');
        $map = ['create_user' => ['eq', session('userId')],'cid'=>$data['cid'], 'status' => ['eq', 0]];
        $list=CartModel::where($map)->find();
        if (empty($list)) {
            $data['create_time'] = time();
            $data['num']=1;
            $info = CartModel::where('status', 0)->insert($data);
            if ($info) {
                return $this->success('添加成功', '', $info);
            } else {
                return $this->error('添加失败');
            }
        }else{
            $info = CartModel::where('id',$list['id'])->setInc('num');
            if ($info) {
                return $this->success('添加成功', '', $info);
            } else {
                return $this->error('添加失败');
            }
        }
    }


    /*
     * 购物车商品 实时 加
     * */
    public function plus(){
        $id=input('id');
        $info = CartModel::where('id',$id)->setInc('num');
        if ($info) {
            $list=CartModel::where('id',$id)->find();
            $list2=CommodityModel::where('id',$list['id'])->find();
            return $this->success('加量成功', '', $list2);
        } else {
            return $this->error('加量失败');
        }
    }


    /*
        * 购物车商品 实时减
        * */
    public function reduce(){
        $id=input('id');
        $info = CartModel::where('id',$id)->setDec('num');
        if ($info) {
            $list=CartModel::where('id',$id)->find();
            $list2=CommodityModel::where('id',$list['id'])->find();
            return $this->success('减量成功', '', $list2);
        } else {
            return $this->error('减量失败');
        }
    }

    /**
     * 地址删除功能
     */
    public function del()
    {
        $id = input('id');
        $data['status'] = '-1';
        $info = CartModel::where('id', $id)->update($data);
        if ($info) {
            return $this->success("删除成功");
        } else {
            return $this->error("删除失败");
        }
    }


}